package com.shark.test.junit;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class MaiZiDownload {

	public static void main(String[] args) {
		new MaiZiDownload().d1();
		
	}
	
	public void d1() {
		for(int i=1;i<=9;i++){
			String url = "http://14.215.100.48/ocs1.maiziedu.com/web3/ajs-0"+i+".mp4" ;
			String name = "ajs-0"+i+".mp4" ;
			
			DownloadMaiZiVideo dv = new DownloadMaiZiVideo(name, url);
			new Thread(dv).start() ;
		}
	}
	
	public void d2() {
		String str = "1-1 window下安装python = http://v1.mukewang.com/2147fd81-e540-499e-8246-df3f5d02814d/H.mp4,"+
				"2-1 Liunx下Python使用 = http://v1.mukewang.com/b6803ae4-cb9d-4713-afd7-f23db4bf3350/H.mp4,"+
				"3-1 python文件类型 = http://v1.mukewang.com/9e6c73e4-6270-412e-a1a3-4e951b7cf823/H.mp4,"+
				"4-1 Eclipse Python配置 = http://v1.mukewang.com/faf365d7-691e-40c3-986f-65d99b496c7a/H.mp4";
		
		String[] split = str.split(",") ;
		for (String s : split) {
			String[] split2 = s.split(" = ");
			System.out.println(split2[0] + "===" + split2[1]);
			
			DownloadMaiZiVideo dv = new DownloadMaiZiVideo(split2[0]+".mp4", split2[1]);
			new Thread(dv).start() ;
		}
		
	}
	
}

class DownloadMaiZiVideo implements Runnable {
	
	private String name ;
	private String url ;
	
	public DownloadMaiZiVideo(String name, String url) {
		this.name = name ;
		this.url = url ;
	}
	
	@Override
	public void run() {
		download(name, url);
	}
	
	public void download(String name, String url) {
		System.out.println(url);
		CloseableHttpClient httpClient = HttpClients.createDefault() ;
		HttpGet httpGet = new HttpGet(url) ;
		CloseableHttpResponse response = null ;
		
		InputStream is = null ;
		BufferedInputStream bis = null ;
		FileOutputStream fos = null ;
		BufferedOutputStream bos = null ;
		try {
			response = httpClient.execute(httpGet) ;
			HttpEntity entity = response.getEntity();
			
			is = entity.getContent() ;
			bis = new BufferedInputStream(is) ;
			
			fos = new FileOutputStream(new File("d:/"+name)) ;
			bos = new BufferedOutputStream(fos) ;
			int len = 0;				
			byte buf[] = new byte[((Long)entity.getContentLength()).intValue()] ;
			while((len=bis.read(buf)) != -1){
				bos.write(buf, 0, len);
			}
			bos.flush(); 
			fos.flush();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
		    try {
				response.close();
				bos.close();
				fos.close();
				bis.close();
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public String getHttpResource(String url) {
		CloseableHttpClient httpClient = HttpClients.createDefault() ;
		HttpGet httpGet = new HttpGet(url) ;
		CloseableHttpResponse response = null ;
		
		StringBuffer strBuf = new StringBuffer() ;
		
		InputStream is = null ;
		BufferedInputStream bis = null ;
		try {
			response = httpClient.execute(httpGet) ;
			HttpEntity entity = response.getEntity();
			
			is = entity.getContent() ;
			bis = new BufferedInputStream(is) ;
			
			byte buf[] = new byte[1024] ;
			while(bis.read(buf) != -1){
				String str = new String(buf) ;
				strBuf.append(str) ;
			}
			
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
		    try {
				response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return strBuf.toString() ;
	}
	
}
